import requests
from lxml import etree
import time
import asyncio
import aiohttp
from functools import lru_cache

# 创建一个全局session以复用连接
session = None

# 使用缓存避免重复请求相同数据
@lru_cache(maxsize=128)
def fetch_match_data_cached(mid, cid):
    return fetch_match_data(mid, cid)

async def fetch_match_data_async(mid, cid):
    start_time = time.time()  # 记录开始时间
    
    # 进球数页面
    url = f'http://vip.titan007.com/changeDetail/overunder.aspx?id={mid}&companyID={cid}&l=0'

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.5845.97 Safari/537.36',
    }
    
    global session
    if session is None:
        connector = aiohttp.TCPConnector(limit=100, ttl_dns_cache=300)
        session = aiohttp.ClientSession(connector=connector)
    
    try:
        # 异步发送请求
        async with session.get(url, headers=headers) as response:
            # 设置编码格式为GBK
            text = await response.text()
            response.encoding = 'gbk'

            # 处理结果
            html_tree = etree.HTML(text)
            
            # 优化XPath查询
            big_small_texts = html_tree.xpath('//table//tr[@bgcolor="#ffffff"][1]/td[4]//text()')

            # 优化中场大小盘数据查询
            mid_big_small_texts = html_tree.xpath('//table[@class="font13"]//tr[td[1][text()="中场"]][1]/td[4]//text()')
            if any(':' in item for item in mid_big_small_texts):
                mid_big_small_texts = html_tree.xpath('//table[@class="font13"]//tr[td[1][text()="中场"]][2]/td[4]//text()')
            
            result = [convert_list_to_string(big_small_texts), convert_list_to_string(mid_big_small_texts)]
            
            end_time = time.time()  # 记录结束时间
            elapsed_time = end_time - start_time  # 计算运行时间
            print(f"fetch_match_data 运行时间: {elapsed_time:.4f} 秒")  # 打印运行时间
            
            return result
    except Exception as e:
        print(f"请求出错: {e}")
        return ["", ""]

def convert_list_to_string(list_data):
    # 使用join方法优化字符串拼接
    return " ".join(list_data)

def fetch_match_data(mid, cid):
    start_time = time.time()  # 记录开始时间
    
    # 进球数页面
    url = f'http://vip.titan007.com/changeDetail/overunder.aspx?id={mid}&companyID={cid}&l=0'

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.5845.97 Safari/537.36',
    }
    
    # 使用Session复用连接
    with requests.Session() as s:
        # 发送请求
        response = s.get(url, headers=headers)
        # 设置编码格式为GBK
        response.encoding = 'gbk'

        # 处理结果
        html_tree = etree.HTML(response.text)

        # 判断是否继续爬取
        a = html_tree.xpath('//table//tr[2]/td[1]//text()')
        ab = [text.strip() for text in a if text.strip()]
        if len(ab)==0:
            print('这个为空')
            return
        
        # 大小盘数据
        big_small_texts = html_tree.xpath('//table//tr[td[@class="hg_red"]][1]/td[position()>=3 and position()<=5]//text()')

        # 中场大小盘数据
        mid_big_small_texts = html_tree.xpath('//table[@class="font13"]//tr[td[1][text()="中场"]][1]/td[position()>=3 and position()<=5]//text()')
        if len(mid_big_small_texts) != 0:
            i = 2
            while i <= 10:  # 设置一个合理的上限避免无限循环
                if mid_big_small_texts and '封' in mid_big_small_texts:
                    mid_big_small_texts = html_tree.xpath(f'//table[@class="font13"]//tr[td[1][text()="中场"]][{i}]/td[position()>=3 and position()<=5]//text()')
                    i += 1
                else:
                    break
        if len(mid_big_small_texts) == 0:
            mid_big_small_texts = html_tree.xpath('//table[@class="font13"]//tr[td[1][text()<="45"]][1]/td[position()>=3 and position()<=5]//text()')
            j = 2
            while j <= 10:  # 设置一个合理的上限避免无限循环
                if mid_big_small_texts and '封' in mid_big_small_texts:
                    mid_big_small_texts = html_tree.xpath(f'//table[@class="font13"]//tr[td[1][text()<="45"]][{j}]/td[position()>=3 and position()<=5]//text()')
                    j += 1
                else:
                    break

        # jinqiushu_tableContent
        try:
            table_elements = html_tree.xpath('//table')
            if table_elements:
                table_html = etree.tostring(table_elements[0], encoding='unicode', pretty_print=True)
                cleaned_html = table_html.replace('&#13;', '').replace('\r', '')
            else:
                cleaned_html = ""
        except Exception as e:
            print(f"处理表格时发生错误: {e}")
            cleaned_html = ""

        result = [convert_list_to_string(big_small_texts), convert_list_to_string(mid_big_small_texts), cleaned_html]


        
        end_time = time.time()  # 记录结束时间
        elapsed_time = end_time - start_time   # 计算运行时间
        print(f"fetch_match_data 运行时间: {elapsed_time:.4f} 秒")  # 打印运行时间
        
        return result

# 提供一个关闭session的方法
async def close_session():
    global session
    if session:
        await session.close()

if __name__ == '__main__':
    mid = 2802706
    cid = 8
    result = fetch_match_data(mid, cid)
    print(result)